# -*- coding: utf-8 -*-
import numpy as np
from algorithm.source_terms.source_base_formula import *
from algorithm.source_terms.flow_patterns.source_base import BaseSource

class BubblyFlowCalculator(BaseSource):
    def __init__(self, standard_input):
        self.standard_input = standard_input
    # def __init__(self, V_L, V_g, V_sL, V_sg, V_r, V_s, alpha_L, alpha_g, rho_L, rho_g, mu_L, mu_g, D, F_E, area, p, theta, eps, sigma):
    #     self.V_L = V_L
    #     self.V_g = V_g
        # self.V_sL = V_sL
        # self.V_sg = V_sg
        # self.V_r = V_r
        # self.V_s = V_s
        # self.alpha_L = alpha_L
        # self.alpha_g = alpha_g
        # self.rho_L = rho_L
        # self.rho_g = rho_g
        # self.mu_L = mu_L
        # self.mu_g = mu_g
        # self.D = D
        # self.F_E = F_E
        # self.area = area
        # self.p = p
        # self.theta = theta
        # self.eps = eps
        # self.sigma = sigma
    

    @staticmethod
    def cal_S_m(D: float) -> float:
        """ 计算界面面积 S_m

        Args:
            D : float 管的内径

        Returns:
            float 界面面积
        """
        return np.pi * D

    @staticmethod
    def cal_rho_m(  
        alpha_g: np.ndarray,
        rho_g: np.ndarray,
        alpha_L: np.ndarray,
        rho_L: np.ndarray,
    ) -> np.ndarray:
        """  计算混合物密度 rho_m

        Args:
            alpha_g :np.ndarray 管道中连续的气体的截面分数
            rho_g :np.ndarray 气体密度
            alpha_L :np.ndarray 管道中连续的液体的截面分数
            rho_L :np.ndarray 液体密度

        Returns:
            np.ndarray
        """
        rho_m = alpha_g * rho_g + (1 - alpha_L) * rho_L
        return rho_m

    @staticmethod
    def cal_mu_m(
        alpha_g: np.ndarray,
        mu_g: np.ndarray,
        alpha_L: np.ndarray,
        mu_L: np.ndarray,
    ) -> np.ndarray:
        """ 计算混合物粘度 mu_m

        Args:
            alpha_g : np.ndarray管道中连续的气体的截面分数
            mu_g : np.ndarray  气体黏度
            alpha_L : np.ndarray 管道中连续的液体的截面分数
            mu_L : np.ndarray  液体黏度

        Returns:
            np.ndarray 混合物粘度
        """
        mu_m = alpha_g * mu_g + (1 - alpha_L) * mu_L
        return mu_m

    @staticmethod
    def cal_lam_m(D: float, re_m: np.ndarray, eps: float) -> np.ndarray:
        """ 计算混合物摩阻系数 lam_m

        Args:
            D : float 管的内径
            re_m : np.ndarray 混合物雷诺数
            eps : float 介质表面粗糙度

        Returns:
            np.ndarray 混合物摩阻系数
        """
        term1 = 1 / 2.8257 * ((eps / D) ** 1.1098) + 5.8506 / re_m**0.8981
        term2 = eps / (3.7065 * D) - 5.0432 / re_m * np.log(term1)
        term3 = -4 * np.log(term2)
        lam_m = (1 / term3) ** 2
        return lam_m

    @staticmethod
    def calculate(self):
       
        """ 计算液体和壁面的摩擦力 gamma_Lw

        Args:
            V_L : np.ndarray 液相速度
            V_g : np.ndarray 气相速度
            alpha_L : np.ndarray 管道中连续的液体的截面分数
            alpha_g : np.ndarray 管道中连续的气体的截面分数
            rho_L : np.ndarray 液体密度
            rho_g : np.ndarray 气体密度
            mu_L : np.ndarray 液体黏度
            mu_g : np.ndarray 气体黏度
            area : float 管道面面积
            D : float 管的内径
            eps : float 介质表面粗糙度

        Returns:
            np.ndarray 液体和壁面的摩擦力
        """
        self.rho_m = BubblyFlowCalculator.cal_rho_m(self.alpha_g, self.rho_g, self.alpha_L, self.rho_L)
        self.mu_m = BubblyFlowCalculator.cal_mu_m(self.alpha_g, self.mu_g, self.alpha_L, self.mu_L)
        #V_m = V_g * alpha_g + V_L * alpha_L
        self.re_m = cal_re(self.rho_m, self.D, self.V_s, self.mu_m)
        self.lam_m = BubblyFlowCalculator.cal_lam_m(self.D, self.re_m, self.eps)
        self.S_m = BubblyFlowCalculator.cal_S_m(self.D)
        self.gamma_Lw = cal_separated_flow_gamma(self.area, self.lam_m, self.rho_m, self.V_s, self.S_m)

        return 0, self.gamma_Lw, 0
        